import pymysql, uuid
# 连接数据库
db = pymysql.connect(host='localhost', port=3307, user='root',
                     passwd='123456', db='test', charset='utf8')
# 创建游标
cursor = db.cursor()
# sql  %s 只是替换,字符串需要额外加""
sql ="insert into user(id, name , sex, age) values ('%s', '%s', %s, %s)"\
     % (str(uuid.uuid1()).replace('-', ""), 'yaozhen', 1, 25)
try:
    cursor.execute(sql)  # 执行
    db.commit()  # 提交
    print("插入成功")
except Exception as e:
    db.rollback()  # 异常回滚
    print("插入失败", e)
db.close()

